package com.clz.latte.net;

import android.content.Context;

import com.clz.latte.net.callback.IError;
import com.clz.latte.net.callback.IFailure;
import com.clz.latte.net.callback.IRequest;
import com.clz.latte.net.callback.ISuccess;
import com.clz.latte.ui.loader.LoaderStyle;

import java.io.File;
import java.util.Map;
import java.util.WeakHashMap;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by Administrator on 2017/11/23.
 */

public class RestClientBuilder {
    private String mUrl;
    private static final Map<String, Object> mParams = RestCreator.getParams();
    private RequestBody mRequestBody;
    private IRequest mIRequest;
    private ISuccess mISuccess;
    private IError mIError;
    private IFailure mIFailure;
    private Context mContext;
    private LoaderStyle mLoaderStyle;
    private File mFile;
    private String mDownLoadDir;
    private String mExension;
    private String mName;

    /**
     * 只允许同包的构造
     */
    RestClientBuilder() {

    }

    public final RestClientBuilder url(String url) {
        this.mUrl = url;
        return this;
    }

    public final RestClientBuilder params(WeakHashMap<String, Object> params) {
        this.mParams.putAll(params);
        return this;
    }

    public final RestClientBuilder downloadDir(String downLoadDir) {
        this.mDownLoadDir = downLoadDir;
        return this;
    }

    public final RestClientBuilder extension(String extension) {
        this.mExension = extension;
        return this;
    }

    public final RestClientBuilder name(String name) {
        this.mName = name;
        return this;
    }

    public final RestClientBuilder params(String key, Object value) {
        mParams.put(key, value);
        return this;
    }

    public final RestClientBuilder raw(String raw) {
        this.mRequestBody =
                RequestBody.create(MediaType.parse("application/json,charset=UTF-8"), raw);
        return this;
    }

    public final RestClientBuilder onRequest(IRequest request) {
        this.mIRequest = request;
        return this;
    }

    public final RestClientBuilder file(File file) {
        this.mFile = file;
        return this;
    }

    public final RestClientBuilder file(String filePath) {
        this.mFile = new File(filePath);
        return this;
    }

    public final RestClientBuilder sucess(ISuccess sucess) {
        this.mISuccess = sucess;
        return this;
    }

    public final RestClientBuilder error(IError error) {
        this.mIError = error;
        return this;
    }

    public final RestClientBuilder failure(IFailure failure) {
        this.mIFailure = failure;
        return this;
    }

    public final RestClientBuilder loader(Context context) {
        this.mLoaderStyle = LoaderStyle.BallClipRotatePulseIndicator;
        this.mContext = context;
        return this;
    }

    public final RestClientBuilder loader(Context context, LoaderStyle style) {
        this.mLoaderStyle = style;
        this.mContext = context;
        return this;
    }

    public final RestClient build() {
        return new RestClient(mUrl,
                mParams, mIRequest, mISuccess
                , mIError, mIFailure, mRequestBody,
                mFile, mDownLoadDir, mExension, mName, mContext, mLoaderStyle);
    }
}
